﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class LoaiSanPham_DAO
    {
        private static LoaiSanPham_DAO m_instance = null;

        public static LoaiSanPham_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new LoaiSanPham_DAO();
                return m_instance;
            }
        }

        public DataTable LayLoaiSP(string maloai)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYLOAISANPHAMBYID", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maloaisp", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maloaisp"].Value = maloai;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public DataTable LayToanBoLoaiSanPham()
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_TOANBO_LOAISANPHAM", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@makhachhang", SqlDbType.Char, 10);
                //cmd.Parameters["@makhachhang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        ///   thêm sản loại sản phẩm mới
        /// </summary>
        /// 
        public bool ThemLoaiSanPham(string _name)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_LOAISANPHAM_MOI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@tenloaisp", SqlDbType.NVarChar, 40);
                cmd.Parameters["@tenloaisp"].Value = _name;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }


        /// <summary>
        ///   Xoa loại sản phẩm mới
        /// </summary>
        /// 

        public bool XoaLoaiSanPham(string _id)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("REMOVE_LOAISANPHAM", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maloaisp", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maloaisp"].Value = _id;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        ///   Sua loai san Pham
        /// </summary>
        /// 
        public bool SuaLoaiSanPham(string _id, string _name)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("UPDATE_LOAISANPHAM", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maloaisp", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maloaisp"].Value = _id;

                cmd.Parameters.Add("@tenloaisp", SqlDbType.NVarChar, 40);
                cmd.Parameters["@tenloaisp"].Value = _name;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
    }
}
